﻿using System.Drawing;
using SSAT.Controls;
using SSAT.ATSDriver;

namespace SSAT.Utils
{
    /// <summary>
    /// Provides access to the ConsoleField control throughout
    /// the application. This control overhear standart console out
    /// and prints it. It is preferable to use this class instead
    /// of Console.
    /// </summary>
    internal static class AppConsole
    {
        private static ConsoleField _consoleField;
        private static Color _separatorColor = Color.DarkRed;

        /// <summary>
        /// Gets or sets associated instance of the ConsoleField. If 
        /// this field is null then nothing will be output.
        /// </summary>
        public static ConsoleField ConsoleField
        {
            get { return _consoleField; }
            set { _consoleField = value; }
        }

        public static Color SeparatorColor
        {
            get { return _separatorColor; }
            set { _separatorColor = value; }
        }

        public static void WriteLine(string text)
        {
            WriteIfConsoleIsNotNull(delegate 
            {
                _consoleField.WriteLine(text); 
            });
        }

        public static void WriteLine(string text, Color color)
        {
            WriteIfConsoleIsNotNull(delegate
            {
                _consoleField.WriteLine(text, color);
            });
        }

        public static void WriteError(string text)
        {
            WriteIfConsoleIsNotNull(delegate
            {
                _consoleField.WriteError(text);
            });
        }

        public static void WriteInfo(string info)
        {
            WriteIfConsoleIsNotNull(delegate
            {
                _consoleField.WriteInfo(info);
            });
        }

        private static void WriteIfConsoleIsNotNull(Action writeMethod)
        {
            if (_consoleField != null)
            {
                writeMethod.Invoke();
            }
        }

        public static void WriteSeparator()
        {
            WriteLine(ATSDriverResources.SeparatingLine, SeparatorColor);
        }

        public static void WriteSeparately(string text)
        {
            WriteSeparator();
            WriteLine(text);
            WriteSeparator();
        }
    }
}
